package com.mzy.linear_struct.ks.回溯;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: mizy3
 * @Date: 2024/08/25/下午7:22
 * @Description:
 */
public class 子集 {

    public List<List<Integer>> subsets(int[] nums) {

        List<List<Integer>> ans = new ArrayList<>();
        Deque<Integer> path = new LinkedList<>();

        backtracking(nums, ans, path, 0);

        return ans;
    }

    private void backtracking(int[] nums, List<List<Integer>> ans, Deque<Integer> path, int start) {
        if(start > nums.length){
            return;
        }
        ans.add(new ArrayList<>(path));
        for (int i = start; i < nums.length; i++) {
            path.addLast(nums[i]);
            backtracking(nums, ans, path, i + 1);
            path.removeLast();
        }
    }

    public static void main(String[] args) {
        System.out.println(new 子集().subsets(new int[]{1, 2, 3}));
    }
}

















